package com.cnpinxi.controller.web.product;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.ProductEntity;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ProductEntityService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */

@RequestMapping("/productEntity")
@Controller
public class ProductEntityController extends BaseController {

    @Autowired
    ProductEntityService productEntityService;

    /**
     * 拉取所有产品实体（没有使用查询图片方法）
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductEntityList")
    public ModelAndView getProductEntityList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/product/getProductEntityList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
        PageInfo<ProductEntity> pageinfo = new PageInfo<>(productEntityList);
        m.addObject("pageinfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }

    /**
     * 跳转到添加或修改页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductEntity")
    @ResponseBody
    public String getProductEntity(HttpServletRequest request) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            Map<String,String> map=super.showRequestParameters(request);
            HttpSession session=request.getSession();
       /*     User user=(User)session.getAttribute(Constant.SESSION_KEY_USER_LOGIN);
            if(user!=null){
                map.put("openId",user.getOpenId());
            }*/

            String openId=(String)session.getAttribute(Constant.SESSION_OPEN_ID);
            if(StringUtils.isNotBlank(openId)){
            logger.info("获取openId。。。。。。。。。。。。。。。。");
                map.put("openId",openId);
            }
            ProductEntity productEntity=   productEntityService.getProductEntity(map);
            resultObj.setMsg("操作成功！");
            resultObj.setData(productEntity);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    /**
     * 添加或修改产品实体
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntitySubmit")
    @ResponseBody
    public String getProductEntitySubmit(HttpServletRequest request, ProductEntity productEntity) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            productEntity=   productEntityService.modifyProductEntity(productEntity);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    @RequestMapping(value = "/productEntityList")
    @ResponseBody
    public String productEntityList(HttpServletRequest request) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            Map<String, String> map = this.showRequestParameters(request);
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
            resultObj.setMsg("操作成功！");
            resultObj.setData(productEntityList);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}
